package edu.wayne.cs.csc4110.hw6.plugins.example.meteor;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

import edu.wayne.cs.csc4110.hw6.framework.Util.RandomUtil;
import edu.wayne.cs.csc4110.hw6.framework.plugin.ActivePlugin;
import edu.wayne.cs.csc4110.hw6.interfaces.user.User;

public class MeteorPlugin extends ActivePlugin {

	private List<Meteor> meteorList = new ArrayList<Meteor>();
	private List<Meteor> removeList = new ArrayList<Meteor>();
	private BufferedImage background;
	private int health;
	private MeteorUser activeUser;
	private static final int GAME_DELAY = 80;

	public MeteorPlugin() {
		super("Meteor Game", new MeteorUserFactory());

	}


	@Override
	public void onStart() {
		activeUser = (MeteorUser) this.getUser();
		health = 100;
		try {
			background = ImageIO.read(((new File("background.png")).toURI())
					.toURL());
		} catch (IOException e) {
			e.printStackTrace();
		}
		for (int i = 0; i < 5; i++) {
			Meteor meteor = new Meteor(RandomUtil.nextInt(20, 50));
			meteorList.add(meteor);
		}

	}

	@Override
	public void update() {
		for (Meteor m : meteorList) {
			m.update();
		}
			checkCollisions();
		if (meteorList.size() < 4) {
			meteorList.add(new Meteor(RandomUtil.nextInt(20, 50)));
		}

	}

	@Override
	public void draw(Graphics g) {
		if (activeUser != null) {
			Graphics2D g2 = (Graphics2D) g;
			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
					RenderingHints.VALUE_ANTIALIAS_ON);

			g2.drawImage(background, 0, 0, null);
			g2.setColor(Color.WHITE);
			g2.drawString("Health:" + health, 0, 10);

			g2.drawString("Score:" + (activeUser.getScore()), 0, 20);
			g2.drawString("Name:" + activeUser.getUsername(), 0, 30);

			drawMeteors(g2);
		}

	}

	private void drawMeteors(Graphics2D g2) {

		for (Meteor m : meteorList) {
			m.draw(g2);
		}
	}

	private void checkCollisions() {
		for (Meteor m : meteorList) {
			if (m.checkCollision(this.getHeight())) {
				decreaseHealth(m.getDestructionValue());
				m.destroy();
				removeList.add(m);
			}
		}
		removeDestroyedMeteors();

	}

	private void decreaseHealth(int destructionValue) {
		health -= destructionValue;
		if (health < 0)
			health = 0;

	}

	private void removeDestroyedMeteors() {
		for (Meteor m : removeList) {
			meteorList.remove(m);
		}
		removeList.clear();
	}

	@Override
	public void keyTyped(KeyEvent e) {
	}

	@Override
	public void keyPressed(KeyEvent e) {
	}

	@Override
	public void keyReleased(KeyEvent e) {
	}

	@Override
	public void mouseClicked(MouseEvent e) {
	}

	private void checkMouseClickInMeteor(MouseEvent e) {
		Meteor toRemove = null;
		for (Meteor m : meteorList) {
			if (m.clickInCircle(e.getPoint())) {
				activeUser.incrementScore(m.getDestructionValue());
				toRemove = m;
			}
		}
		if (toRemove != null)
			meteorList.remove(toRemove);
	}

	@Override
	public void mousePressed(MouseEvent e) {
		checkMouseClickInMeteor(e);
	}

	@Override
	public void mouseReleased(MouseEvent e) {
	}

	@Override
	public void mouseEntered(MouseEvent e) {
	}

	@Override
	public void mouseExited(MouseEvent e) {
	}

	@Override
	public void mouseDragged(MouseEvent e) {
	}

	@Override
	public void mouseMoved(MouseEvent e) {
	}

	@Override
	public int getDelay() {
		return GAME_DELAY;
	}

	@Override
	public Cursor getCursor() {
		return new Cursor(Cursor.CROSSHAIR_CURSOR);

	}

	@Override
	public void onFinish() {
		meteorList.clear();
	}

	@Override
	public boolean isOver() {
		return health == 0;
	}

	@Override
	public ActivePlugin newInstance(User u) {
		MeteorPlugin tempInstance = new MeteorPlugin();
		tempInstance.setUser(u);
		return tempInstance;
	}

	@Override
	public void restart() {
		this.meteorList.clear();
		this.health = 100;
	}



}
